package com.leetcode.partition2;

/**
 * @author `RKC`
 * @date 2021/8/23 10:17
 */
public class LC198打家劫舍 {

    public static int rob(int[] nums) {
        return dynamicProgramming(nums);
    }

    public static void main(String[] args) {
        int[] nums = { 2, 7, 9, 3, 1 };
        System.out.println(rob(nums));
    }

    private static int dynamicProgramming(int[] nums) {
        if (nums.length == 1) return nums[0];
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        dp[1] = Math.max(nums[0], nums[1]);
        for (int i = 2; i < dp.length; i++) {
            //每家都有两种情况，要么偷，要么不偷。如果偷，当前最大收益就是dp[i-2]+nums[i]，不偷则采用上一次的方案
            dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
        }
        return dp[dp.length - 1];
    }
}
